<?php

namespace App\Domain\Material\Dao;

use App\Domain\Material\Models\MaterialModel;

class MaterialDao
{
    public static function findById($id)
    {
        return MaterialModel::where('id', $id)->where('state', '1')->first();
    }

    public static function findBySN($SN)
    {
        return MaterialModel::where('SN', $SN)->where('state', '1')->first();
    }

    public static function findBySNs($SN)
    {
        return MaterialModel::whereIn('SN', $SN)->where('state', '1')->get();
    }

    public static function findByMaterialDictCode($id)
    {
        return MaterialModel::where('material_dict_code', $id)->where('state', '1')->get();
    }

    public static function findBySNAndModel($SN, $model)
    {
        return MaterialModel::where('SN', $SN)->where('model', $model)->where('state', '1')->first();
    }

    public static function findBySNsAndDistributionStatus($SN)
    {
        return MaterialModel::where('SN', $SN)->where('distribution_status', '1')->where('state', '1')->first();
    }

    public function del($SN)
    {
        return MaterialModel::whereIn('SN', $SN)->update(['state' => '0']);
    }

    public function findByDictAndChannelID($param)
    {
        return MaterialModel::where('material_dict_code', $param['material_dict_code'])->where('channel_business_id', $param['channel_business_id'])->where('state', '1')->get();
    }

    public function distributionDel($id)
    {
        return MaterialModel::where('id', $id)->update(['distribution_status' => '', 'channel_business_id' => '']);
    }

    /**
     * 手动入库
     * @param $params
     * @return MaterialModel
     */
    public function create($params)
    {
        $params = array_filter_null($params);
        return MaterialModel::create($params);
    }

    /**
     * 铺货管理
     * @param MaterialModel $model
     * @param $param
     * @return MaterialModel
     */
    public function update(MaterialModel $model, $param)
    {
        $param = array_filter_null($param);
        $model->fill($param)->save();
        return $model;
    }
}
